package app.controllers.dishes;

import app.controllers.FileController;
import app.models.dishes.Category;
import app.models.dishes.Dish;
import app.models.system.Attachments;
import com.ifonly.activerecord.ActiveRecordFactory;
import com.ifonly.activerecord.DbUtils;
import com.ifonly.activerecord.Record;
import com.ifonly.activerecord.sql.config.Sql;
import com.ifonly.activerecord.tx.TxInterceptor;
import com.ifonly.interceptor.Intercepts;
import com.ifonly.mvc.ActionKey;
import com.ifonly.mvc.Controller;
import com.ifonly.utils.ArrayUtils;

import java.util.List;

/**
 * @author ifonly
 * @version 1.0 2016-02-17 16:10
 * @since JDK 1.6
 */
public class DishController extends Controller {
    private Attachments dao = ActiveRecordFactory.getActiveRecord(Attachments.class);

    @ActionKey("")
    public void index() {
        List<Category> categories = DbUtils.list(Category.class, Sql.get("category.list"));
        setAttr("categories", categories);
        render("index");
    }

    public void item() {
        long id = getLong(0);
        Dish dish;
        if (id == 0) {
            dish = new Dish();
        } else {
            dish = Dish.findByPrimaryKey(Dish.class, id);
        }

        List<Record> tags = DbUtils.list(Sql.get("tag.listWithDish"), id);
        List<Category> categories = DbUtils.list(Category.class, Sql.get("category.list"));

        setAttr("dish", dish);
        setAttr("tags", tags);
        setAttr("categories", categories);
        render("item");
    }

    @Intercepts(TxInterceptor.class)
    public void save() {
        String[] dish_tags = getParams("dish_tags");
        Dish dish = getModel(Dish.class, "dish");
        if (DbUtils.isNew(dish)) {
            dish.saveReturnGeneratedKey();
        } else {
            dish.update();
            DbUtils.delete(Sql.get("dish.deleteTags"), dish.getLong("id"));
        }

        long dishId = dish.getLong("id");
        if (!ArrayUtils.isNullOrEmpty(dish_tags)) {
            int length = dish_tags.length;
            Object[][] params = new Object[length][];
            for (int i = 0; i < length; i++) {
                params[i] = new Object[]{
                        dishId,
                        dish_tags[i]
                };
            }
            DbUtils.insertBatch(Sql.get("dish.insertTags"), params);
        }


        redirect("");
    }

    public void dt() {
        renderDataTables(Sql.get("dish.segment"), "WHERE");
    }

    public void status() {
        long id = getLong(0);
        long status = getInt(1);
        DbUtils.update(Sql.get("dish.status"), status, id);
        renderSuccess();
    }

    @Intercepts(TxInterceptor.class)
    public void delete() {
        long id = getLong(0);
        Dish dish = DbUtils.findByPrimaryKey(Dish.class, id);
        //删除上传的文件
        //1. 删除封面
        dao.delete(FileController.getUploadDir(getRequest()), dish.getLong("cover"));
        //2. 删除关联图片
        dao.delete(FileController.getUploadDir(getRequest()), dish.getString("pictures"));
        //删除菜品标签关联
        DbUtils.delete(Sql.get("dish.deleteTags"), id);
        //删除菜品信息
        dish.delete();
        renderSuccess();
    }

    public void cover() {
        long id = getLong("id");
        DbUtils.update(Sql.get("dish.cover"), id);
        renderSuccess();
    }

}
